package com.biyu.monitormanage.controller;

import com.biyu.monitormanage.common.annotation.SysLog;
import com.biyu.monitormanage.common.utils.BaseResponse;
import com.biyu.monitormanage.common.utils.PageUtils;
import com.biyu.monitormanage.entity.MachineEntity;
import com.biyu.monitormanage.entity.MonitorEntity;
import com.biyu.monitormanage.service.MonitorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author 15491
 */
@RestController
@RequestMapping("monitormanage/monitor")
public class MonitorController {

    private final MonitorService monitorService;

    @Autowired
    public MonitorController(MonitorService monitorService) {
        this.monitorService = monitorService;
    }

    @GetMapping("/query")
    @RequiresPermissions("monitormanage:monitor:query")
    public BaseResponse queryMonitorList(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = monitorService.queryMonitor(params);
        return BaseResponse.ok().put("page", pageUtils);
    }

    /**
     * 用户信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("monitormanage:monitor:info")
    public BaseResponse info(@PathVariable("id") Long id){
        MonitorEntity monitor = monitorService.getById(id);

        return BaseResponse.ok().put("user", monitor);
    }

    /**
     * 保存
     */
    @SysLog("新增监测仪器信息")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @RequiresPermissions("monitormanage:monitor:save")
    public BaseResponse save(@RequestBody MonitorEntity monitor){
        monitorService.save(monitor);

        return BaseResponse.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改监测仪器信息")
    @RequestMapping("/update")
    @RequiresPermissions("monitormanage:monitor:update")
    public BaseResponse update(@RequestBody MonitorEntity monitor){
        monitorService.updateById(monitor);
        return BaseResponse.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除监测仪器")
    @RequestMapping("/delete")
    @RequiresPermissions("monitormanage:monitor:delete")
    public BaseResponse delete(@RequestBody Long[] ids){
        monitorService.removeByIds(Arrays.asList(ids));

        return BaseResponse.ok();
    }
}
